<template>
  <div style="width: 97%;margin: 0 auto">
    <div class="search">
      <div style="margin-left: 16px;margin-top: 10px;">
        <i class="el-icon-s-custom" ></i>药品库
      </div>
      <div class="int">
        <el-form :model="queryParams" ref="queryForm" :inline="true"  label-width="68px">
          <input type="text"  placeholder="请输入规范名称" v-model="queryParams.name" style="height: 30px;width: 252px">
          <input type="text"  placeholder="请输入药品编码" v-model="queryParams.code" style="height: 30px;width: 252px">
          <input type="text"  placeholder="拼音码" v-model="queryParams.pinyinCode" style="height: 30px;width: 252px">
          <input type="text"  placeholder="常用名称" v-model="queryParams.commonName"  style="height: 30px;width: 252px">
        </el-form>
      </div>
      <div class="inquire">
        <el-button type="success" icon="el-icon-search" size="mini" @click="handleQuery">查询</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
<!--        <el-button type="success" style="height: 30px;display: flex;align-items: center;float:left;margin-right: 20px">-->
<!--          <i  class="el-icon-search"></i>-->
<!--          查询</el-button>-->
<!--        <el-button style="height: 30px;display: flex;align-items: center;" @click="reset">-->
<!--          <i  class="el-icon-refresh"></i>-->
<!--          重置</el-button>-->
      </div>
    </div>
    <div class="list">
      <div style="margin-left: 10px;margin-top: 10px;margin-bottom: 10px;overflow:hidden;">
<!--        <el-button type="primary"  style="height: 35px;display: flex;align-items: center;float:left;margin-left: 10px" @click="addition">-->
<!--          <i class="el-icon-plus"></i>添加</el-button>-->
<!--        <el-button type="success"  style="height: 35px;display: flex;align-items: center;float:left;margin-left: 20px" >-->
<!--          <i class="el-icon-edit-outline"></i>修改</el-button>-->
<!--        <el-button type="danger"  style="height: 35px;display: flex;align-items: center;float:left;margin-left: 20px">-->
<!--          <i class="el-icon-delete"></i>删除</el-button>-->
        <el-col :span="1.5">
          <el-button
            style="margin-left: 20px; width: 80px;height: 32px"
            type="primary"
            plain
            icon="el-icon-plus"
            size="mini"
            @click="handleAdd"
            v-hasPermi="['system:drug:add']"
          >新增</el-button>
        </el-col>

        <el-col :span="1.5">
          <el-button
            style="margin-left: 20px; width: 80px;height: 32px"
            type="success"
            plain
            icon="el-icon-edit"
            size="mini"
            :disabled="single"
            @click="handleUpdate"
            v-hasPermi="['system:drug:edit']"
          >修改</el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button
            style="margin-left: 20px; width: 80px;height: 32px"
            type="danger"
            plain
            icon="el-icon-delete"
            size="mini"
            :disabled="multiple"
            @click="handleDelete"
            v-hasPermi="['system:drug:remove']"
          >删除</el-button>
        </el-col>

        <el-button
          style="margin-left: 20px; width: 80px;height: 32px"
          type="warning"
          plain
          icon="el-icon-upload2"
          size="mini"
          @click="handleImport"
          v-hasPermi="['system:drug:import']"
        ><i class="el-icon-upload"></i>导入</el-button>

<!--        <el-button type="warning"  style="height: 35px;display: flex;align-items: center;float:left;margin-left: 20px" @click="uploading">-->
<!--          <i class="el-icon-upload"></i>导入</el-button>-->
      </div>
      <el-table v-loading="loading" :data="userList" @selection-change="handleSelectionChange">

        <el-table-column
          type="selection"
          align="center"
          width="50">
        </el-table-column>
        <el-table-column
          prop="id"
          label="序号"
          align="center"
          width="100"
        >
        </el-table-column>
        <el-table-column
          prop="code"
          label="用药编码"
          align="center"
          width="350">
        </el-table-column>
        <el-table-column
          prop="name"
          label="标准名称"
          align="center"
          width="350">
        </el-table-column>
        <el-table-column
          prop="pinyinCode"
          label="拼音码"
          align="center"
          width="350">
        </el-table-column>
        <el-table-column
          prop="commonName"
          label="常用名称"
          align="center"
          width="262">
        </el-table-column>
      </el-table>
      <!--      <div style="position: absolute;right: 30px;bottom: 20px;">-->

      <div style="position: fixed;right: 30px;bottom: 60px;">

        <pagination
          v-show="total>0"
          :total="total"
          :page.sync="queryParams.pageNum"
          :limit.sync="queryParams.pageSize"
          @pagination="druglist"
        />

<!--        <el-pagination-->
<!--          @size-change="handleSizeChange"-->
<!--          @current-change="handleCurrentChange"-->
<!--          :current-page="currentPage"-->
<!--          :page-sizes="[3, 5, 10]"-->
<!--          :page-size="pagesize"-->
<!--          layout=" prev, pager, next,sizes, jumper"-->
<!--          :total="tableData.length">-->
<!--        </el-pagination>-->
      </div>
      <!--    修改-->
      <!-- 添加或修改参数配置对话框 -->
      <el-dialog :title="title" :visible.sync="open" width="600px" style="margin-top: 200px" append-to-body>
        <el-form ref="form" :model="form" :rules="rules" label-width="80px">
          <el-row>
            <el-col :span="24">
              <el-form-item label="用药编码" prop="code">
                <el-input v-model="form.code" placeholder="请输入" />
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="24">
              <el-form-item label="标准名称" prop="name">
                <el-input v-model="form.name" placeholder="请输入" />
              </el-form-item>
            </el-col>
          </el-row>

          <el-row>
            <el-col :span="24">
              <el-form-item label="拼音码" prop="pinyinCode">
                <el-input v-model="form.pinyinCode" placeholder="请输入" />
              </el-form-item>
            </el-col>
          </el-row>

          <el-row>
            <el-col :span="24">
              <el-form-item label="常用名称" prop="commonName">
                <el-input v-model="form.commonName" placeholder="请输入" />
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>

        <div slot="footer" class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </el-dialog>
<!--      <div class="alter" v-show="fage">-->
<!--        <div class="hint"><p>添加药品</p> <i class="el-icon-close" @click="abrogate"></i></div>-->
<!--        <div>-->
<!--          <el-form ref="form"  label-width="80px" style="overflow: hidden" :model="form" >-->
<!--            <el-form-item label="用药编码" prop="name" style="margin-top: 30px;margin-left:30px">-->
<!--              <el-input class="txt" v-model="form.name" style="width: 300px;"></el-input>-->
<!--            </el-form-item>-->
<!--            <el-form-item label="标准名称" prop="standard" style="margin-top: 30px;margin-left: 30px">-->
<!--              <el-input class="txt" v-model="form.standard" style="width: 300px;"></el-input>-->
<!--            </el-form-item>-->
<!--            <el-form-item label="拼音码" prop="spell" style="margin-top: 30px;margin-left: 30px">-->
<!--              <el-input class="txt" v-model="form.spell" style="width: 300px;"></el-input>-->
<!--            </el-form-item>-->
<!--            <el-form-item label="常用名称" prop="designation" style="margin-top: 30px;margin-left: 30px">-->
<!--              <el-input class="txt" v-model="form.designation" style="width: 300px;"></el-input>-->
<!--            </el-form-item>-->
<!--                    <div style="width: 200px;margin: 0 auto">-->
<!--                      <el-button type="success" style="height: 30px;display: flex;align-items: center;float:left;margin-right: 20px">-->
<!--                        确认</el-button>-->
<!--                      <el-button style="height: 30px;display: flex;align-items: center;" @click="abrogate">-->
<!--                        取消</el-button>-->
<!--                    </div>-->
<!--          </el-form>-->
<!--        </div>-->
<!--      </div>-->
    </div>

    <!--上传窗口-->
    <el-dialog :title="upload.title" :visible.sync="upload.open" width="400px" style="margin-top: 200px" append-to-body>
      <el-upload
        ref="upload"
        :limit="1"
        accept=".xlsx, .xls"
        :headers="upload.headers"
        :action="upload.url + '?updateSupport=' + upload.updateSupport"
        :disabled="upload.isUploading"
        :on-progress="handleFileUploadProgress"
        :on-success="handleFileSuccess"
        :auto-upload="false"
        drag
      >
        <i class="el-icon-upload"></i>
        <div class="el-upload__text">
          将文件拖到此处，或
          <em>点击上传</em>
        </div>
        <div class="el-upload__tip" slot="tip">
          <el-checkbox v-model="upload.updateSupport" />
          <el-link type="info" style="font-size:12px" @click="importTemplate">下载模板</el-link>
        </div>
        <div class="el-upload__tip" style="color:red" slot="tip">提示：仅允许导入“xls”或“xlsx”格式文件！</div>
      </el-upload>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitFileForm">确 定</el-button>
        <el-button @click="upload.open = false">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import { druglist,getDetail,addDrug, updateDrug, delDrug ,importTemplate  } from "../../api/drug/drug";
  import {getToken} from "@/utils/auth";

  export default {
  name: 'PatientManagement',
  data () {
    return {
      form: {
        id:'' ,
        name: '',
        code: '',
        pinyinCode: '',
        commonName: ''
      },
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 选中数组
      ids: [],
      // 总条数
      total: 0,
      // 用户表格数据
      userList: null,
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        name: undefined,
        code: undefined,
        pinyinCode: undefined,
        commonName: undefined
      },

      // 遮罩层
      loading: true,

      // 用户导入参数
      upload: {
        // 是否显示弹出层（用户导入）
        open: false,
        // 弹出层标题（用户导入）
        title: "药品导入",
        // 是否禁用上传
        isUploading: false,
        // 是否更新已经存在的用户数据
        updateSupport: 0,
        // 设置上传的请求头部
        headers: { Authorization: "Bearer " + getToken() },
        // 上传的地址
        url: process.env.VUE_APP_BASE_API + "/system/drug/importData"
      },

      fage: false,
      currentPage: 1,
      pagesize: 10,
      enable: true,
      labelPosition: 'right',
      tableData: [ ],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 表单校验
      rules: {
        name: [
          { required: true, message: "名称不能为空", trigger: "blur" },
          {pattern: /^[\a-\z\A-\Z0-9\u4e00-\u9fe5]+$/,required: true , message: "名称只能输入中文、数字和英文",trigger: "blur"}
        ],
        code: [
          { required: true, message: "编码不能为空", trigger: "blur" },
          {pattern:/^[a-zA-Z0-9]+$/ ,required: true , message: "编码不能为中文",trigger: "blur"}

        ],
        commonName: [
          { required: true, message: "常用名称不能为空", trigger: "blur" },
          {pattern: /^[\a-\z\A-\Z0-9\u4e00-\u9fe5]+$/,required: true , message: "常用名称只能输入中文、数字和英文",trigger: "blur"}

        ],
        pinyinCode: [
          {type:'string', required: true, message: "拼音码不能为空", trigger: "blur" },
          {pattern:/^[A-Za-z]+$/,required: true , message: "只能输入英文字母",trigger: "blur"}
        ]

      }
    }
  },
    computed:{
      // druglist(){
      //   druglist().then(res=>{
      //     console.log(res.data)
      //   }).catch(err=>{
      //     console.log(err)
      //   })
      // },
    },
    created(){
      this.druglist()
    },
  methods: {

    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.page = 1;
      this.druglist();
    },

    druglist(){
      this.loading = true;
      druglist( this.addDateRange(this.queryParams) ).then(response=>{
        console.log("response.rows",response.rows)
        this.userList = response.rows;
        this.total = response.total;
        this.loading = false;
      }).catch(err=>{
        console.log(err)
      })
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.queryParams.name = ''
      this.queryParams.code = ''
      this.queryParams.pinyinCode = ''
      this.queryParams.commonName = ''
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id);
      this.single = selection.length != 1;
      this.multiple = !selection.length;
    },

    addition () {
      this.fage = true
    },
    abrogate () {
      this.fage = false
      this.$refs.form.resetFields()
    },
    details (index) {
      console.log(1)
      console.log(index)
    },
    handleSizeChange (val) {
      this.pagesize = val
      console.log(`每页 ${val} 条`)
    },
    handleCurrentChange (val) {
      this.currentPage = val
      console.log(`当前页: ${val}`)
    },
    // 查看
    editshow () {
      console.log('跳转')
      // this.$router.push('/HomePage/PhysicalNavigation/Physicallook')
    },
    // 上传
    uploading () {
      this.$router.push('/Drug/DrugUploading')
    },

    tableRowClassName ({ row, rowIndex }) {
      if (rowIndex === 1) {
        return 'warning-row'
      } else if (rowIndex === 3) {
        return 'success-row'
      }
      return ''
    } ,
    /** 删除按钮操作 */
    handleDelete(row) {
      console.log(this.ids)
      // debugger
      const userIds = row.id || this.ids;
      this.$confirm('是否确认删除序号为"' + userIds + '"的数据项?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(function() {
        return delDrug(userIds);
      }).then(() => {
        this.druglist();
        this.msgSuccess("删除成功");
      })
    },

    /** 导入按钮操作 */
    handleImport() {
      this.upload.title = "药品导入";
      this.upload.open = true;
    },
    /** 下载模板操作 */
    importTemplate() {
      console.log("....download ")
      importTemplate().then(response => {
        this.download(response.msg);
      });
    },
    // 文件上传中处理
    handleFileUploadProgress(event, file, fileList) {
      this.upload.isUploading = true;
    },
    // 文件上传成功处理
    handleFileSuccess(response, file, fileList) {
      this.upload.open = false;
      this.upload.isUploading = false;
      this.$refs.upload.clearFiles();
      this.$alert(response.msg, "导入结果", { dangerouslyUseHTMLString: true });
      this.getList();
    },
    // 提交上传文件
    submitFileForm() {
      this.$refs.upload.submit();
    } ,

    /** 新增按钮操作 */
    handleAdd() {
       this.reset();
      getDetail(0).then(response => {
        // console.log('新增按钮操作',response)
        this.open = true;
        this.title = "修改药品";
      });
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const ids = row.id || this.ids;
      getDetail(ids).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改药品";
      });
    },
    // 表单重置
    reset() {
      this.form = {
        id:undefined,
        name: undefined,
        code: undefined,
        pinyinCode: undefined,
        commonName: undefined
      };
      this.resetForm("form");
    },
    /** 提交按钮 */
    submitForm: function() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != undefined) {
            updateDrug(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.druglist();
            });
          } else {
            addDrug(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.druglist();
            });
          }
        }
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },

  }
}
</script>

<style scoped lang="scss">
  .alter{
    background: #FFFFFF;
    box-shadow: 0px 0px 32px 0px rgba(97,97,97,0.1);
    border-radius: 8px;
    width: 500px;
    height: 420px;
    position: absolute;
    top: 10px;
    bottom: 0;
    left: 0;
    right: 0;
    margin: 0 auto;
    z-index: 10;
    overflow: hidden;
    .hint{
      width: 100%;
      height: 40px;
      line-height: 40px;
      background: #F8F8F8;
      border-radius: 8px 8px 0px 0px;
      overflow: hidden;
      p{
        float: left;
        margin-left: 20px;
        margin-top: -1px;
      }
      i{
        float: right;
        margin-right: 20px;
        margin-top: 12px;
      }
    }
  }
  .submit{
    width: 80px;
    height: 30px;
    background: #009945;
    border-radius: 4px;
    color: #ffffff;
    text-align: center;
    line-height: 30px;
    font-size: 12px;
    float: left;
    cursor:pointer;
  }
  .cancel{
    width: 80px;
    float: left;
    height: 30px;
    border-radius: 4px;
    text-align: center;
    line-height: 30px;
    font-size: 12px;
    border: 1px solid #EEEEEE;
    margin-left: 20px;
    cursor:pointer;
  }
  .redact{
    width: 600px;
    height: 580px;
    background: #FFFFFF;
    box-shadow: 0px 0px 32px 0px rgba(97,97,97,0.1);
    border-radius: 8px;
    position:fixed;
    top: 50%;
    left: 50%;
    margin-left: -300px;
    margin-top: -285px;
    z-index: 10;
    .title{
      width: 100%;
      height: 40px;
      background: #e7e5e5;
      /*background:red;*/
      border-radius: 8px 8px 0px 0px;
    }
  }
  .search{
    width: 100%;
    height: 120px;
    background: #ffffff;
    border-radius: 8px;
    overflow: hidden;
    box-shadow: 0px 4px 20px 0px rgba(51,51,51,0.08);
    margin-top: 20px;
  }
  .list{
    width: 100%;
    height: 670px;
    background: #ffffff;
    box-shadow: 0px 4px 20px 0px rgba(51,51,51,0.08);
    border-radius: 8px;
    margin-top: 20px;
    position: relative;
    overflow: hidden;
  }
  .int{
    width: 75%;
    float: left;
    input{
      padding: 0px 10px;
      width: 225px;
      height: 30px;
      background: #FFFFFF;
      border: 1px solid #E8E8E8;
      border-radius: 4px;
      margin-left: 20px;
      margin-top:20px;
    }
  }
  .inquire{
    width: 20%;
    height: 50px;
    float: right;
    margin-top: 20px;
    .el-button{
      width: 31%;
      height: 30px;
    }
  }

</style>
